package com.dyh.quickdemozwei;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yhdong
 * @Date: 2021/10/8 11:49
 */
public class SimpleDateFormatThreadSupport {
    private static final ThreadLocal<Map<String, SimpleDateFormat>> tl = ThreadLocal.withInitial(
            HashMap::new
    );

    private static String defaultFormat = "yyyy-MM-dd HH:mm:ss";

    public static SimpleDateFormat getInstance() {
        return getInstance(defaultFormat);
    }

    public static SimpleDateFormat getInstance(String format) {
        SimpleDateFormat sdf = tl.get().get(format);
        if (null == sdf) {
            sdf = new SimpleDateFormat(format);
            tl.get().put(format, sdf);
        }
        return sdf;
    }
}
